//
//  TXAdapter.h
//  TXAdapter
//
//  Created by Yao on 2021/1/6.
//  Copyright © 2021 Yao. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//#import "TheOne_HeaderConfig.h"

/** 适配手机类型 */
typedef NS_ENUM(NSInteger,TXAdapterPhoneType) {
    /** iPhone3GS_4_4S */
    TXAdapterPhoneType_iPhone3GS_4_4S    = 0,
    /** iPhone5_5C_5S_5SE */
    TXAdapterPhoneType_iPhone5_5C_5S_5SE = 1,
    /** iPhone6_6S_7_8_SE_SE2_SE3 */
    TXAdapterPhoneType_iPhone6_6S_7_8_SE_SE2_SE3 = 2,
    /** iPhone6Plus_6SPlus_7Plus_8Plus */
    TXAdapterPhoneType_iPhone6Plus_6SPlus_7Plus_8Plus = 3,
    /** iPhoneX_XS_11Pro_12mini_13mini */
    TXAdapterPhoneType_iPhoneX_XS_11Pro_12mini_13mini = 4,
    /** iPhoneXSMax_XR_11_11ProMax */
    TXAdapterPhoneType_iPhoneXSMax_XR_11_11ProMax     = 5,
    /** iPhone12_12Pro_13_13Pro_14 */
    TXAdapterPhoneType_iPhone12_12Pro_13_13Pro_14_15     = 6,
    /** iPhone12ProMax_13ProMax_14Plus */
    TXAdapterPhoneType_iPhone12ProMax_13ProMax_14Plus_15Plus = 7,
    /** iPhone14Pro 15ProMax */
    TXAdapterPhoneType_iPhone14Pro_15Pro                   = 8,
    /** iPhone14ProMax 15ProMax */
    TXAdapterPhoneType_iPhone14ProMax_15ProMax                 = 9,
    /** 其他 */
    TXAdapterPhoneTypeOther                           = 10,
};

/** 所需适配机型-屏幕宽 */
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone3GS_4_4S;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone5_5C_5S_5SE;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone6_6S_7_8_SE_SE2_SE3;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone6Plus_6SPlus_7Plus_8Plus;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhoneX_XS_11Pro_12mini_13mini;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhoneXSMax_XR_11_11ProMax;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone12_12Pro_13_13Pro_14_15;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone12ProMax_13ProMax_14Plus_15Plus;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone14Pro_15Pro;
UIKIT_EXTERN CGFloat const SCREEN_WIDTH_iPhone14ProMax_15ProMax;

/** 所需适配机型-屏幕高 */
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone3GS_4_4S;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone5_5C_5S_5SE;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone6_6S_7_8_SE_SE2_SE3;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone6Plus_6SPlus_7Plus_8Plus;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhoneX_XS_11Pro_12mini_13mini;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhoneXSMax_XR_11_11ProMax;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone12_12Pro_13_13Pro_14_15;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone12ProMax_13ProMax_14Plus_15Plus;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone14Pro_15Pro;
UIKIT_EXTERN CGFloat const SCREEN_HEIGHT_iPhone14ProMax_15ProMax;

/** 屏幕宽度 */
static inline CGFloat tScreenWidth() {
    return [UIScreen mainScreen].bounds.size.width;
}

/** 幕高度 */
static inline CGFloat tScreenHeight() {
    return [UIScreen mainScreen].bounds.size.height;
//    //12表示底部的宽度距离
//    
//    BOOL isPhoneX = NO;
//    
//    if (@available(iOS 11.0, *)) {
//    isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;
//    }
//    if (isPhoneX) {
//        return 780+34-12;
//    }
//    return 780-12;
    
}

/** 当前屏幕类型 */
static inline TXAdapterPhoneType tCurrentType() {
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone3GS_4_4S) return TXAdapterPhoneType_iPhone3GS_4_4S;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone5_5C_5S_5SE) return TXAdapterPhoneType_iPhone5_5C_5S_5SE;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone6_6S_7_8_SE_SE2_SE3) return TXAdapterPhoneType_iPhone6_6S_7_8_SE_SE2_SE3;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone6Plus_6SPlus_7Plus_8Plus) return TXAdapterPhoneType_iPhone6Plus_6SPlus_7Plus_8Plus;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhoneX_XS_11Pro_12mini_13mini) return TXAdapterPhoneType_iPhoneX_XS_11Pro_12mini_13mini;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhoneXSMax_XR_11_11ProMax) return TXAdapterPhoneType_iPhoneXSMax_XR_11_11ProMax;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone12_12Pro_13_13Pro_14_15) return TXAdapterPhoneType_iPhone12_12Pro_13_13Pro_14_15;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone12ProMax_13ProMax_14Plus_15Plus) return TXAdapterPhoneType_iPhone12ProMax_13ProMax_14Plus_15Plus;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone14Pro_15Pro) return TXAdapterPhoneType_iPhone14Pro_15Pro;
    if (tScreenHeight() == SCREEN_HEIGHT_iPhone14ProMax_15ProMax) return TXAdapterPhoneType_iPhone14ProMax_15ProMax;
    return TXAdapterPhoneTypeOther;
}

/** 屏幕适配 */
@interface TXAdapter : NSObject

/** 屏幕默认类型 默认为TXAdapterPhoneType_iPhone6_6S_7_8 */
@property(nonatomic)TXAdapterPhoneType defaultType;

/** 屏幕宽度 */
@property(nonatomic,readonly)CGFloat defaultScreenWidth;

/** 屏幕高度 */
@property(nonatomic,readonly)CGFloat defaultScreenHeight;

/** 共享适配器 */
+ (instancetype)shareAdapter;

@end

/**
 注：屏幕及字体是以屏幕宽度来适配的
 */

/** 真实字体大小 */
static inline CGFloat tRealFontSize(CGFloat defaultSize) {
    if ([TXAdapter shareAdapter].defaultType == tCurrentType())
        return defaultSize;
    
    NSString *deviceType = [UIDevice currentDevice].model;
    if ([deviceType isEqualToString:@"iPad"]) {
        
        CGFloat xx =  [UIScreen mainScreen].bounds.size.height / tScreenHeight() * defaultSize;
        NSLog(@"%f",xx);
        return xx;
    }
    
    BOOL isPhoneX = NO;
    if (@available(iOS 11.0, *)) {
        isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;
    }

    if (isPhoneX) {
        
        if ([UIScreen mainScreen].bounds.size.height == SCREEN_HEIGHT_iPhoneX_XS_11Pro_12mini_13mini) {
            return defaultSize;
        }else{
            return [UIScreen mainScreen].bounds.size.height / tScreenHeight() * defaultSize;
        }
    }else
    {
        if ([UIScreen mainScreen].bounds.size.width<=SCREEN_HEIGHT_iPhone6Plus_6SPlus_7Plus_8Plus) {
            return defaultSize;
        }
    }
    
    return [UIScreen mainScreen].bounds.size.height / tScreenHeight() * defaultSize;
    
    //多出116的高度
    
}


/**  */
static inline CGFloat tRealLength(CGFloat defaultLength) {
    if ([TXAdapter shareAdapter].defaultType == tCurrentType())
        return defaultLength;

//    NSLog(@"%f",[TXAdapter shareAdapter].defaultScreenHeight);
//
//    NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
    NSString *deviceType = [UIDevice currentDevice].model;
    
    BOOL isPhoneX = NO;
    if (@available(iOS 11.0, *)) {
        isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;
    }
    
    
    if ([deviceType isEqualToString:@"iPad"]) {
        
        CGFloat xx =  [UIScreen mainScreen].bounds.size.height / tScreenHeight() * defaultLength;
        NSLog(@"%f",xx);
        return xx;
    }
    
    if ([UIScreen mainScreen].bounds.size.height == SCREEN_HEIGHT_iPhoneX_XS_11Pro_12mini_13mini) {
        return defaultLength;
    }

    if (isPhoneX) {
        return [UIScreen mainScreen].bounds.size.height/tScreenHeight() * defaultLength;
    }else{
        if ([UIScreen mainScreen].bounds.size.height<=SCREEN_HEIGHT_iPhone6_6S_7_8_SE_SE2_SE3 || [UIScreen mainScreen].bounds.size.width<=SCREEN_HEIGHT_iPhone6Plus_6SPlus_7Plus_8Plus) {
            return defaultLength;
        }
    }
    return [UIScreen mainScreen].bounds.size.height/tScreenHeight() * defaultLength;
}

/** 实际宽度*/
static inline CGFloat tRealWidth(CGFloat defaultLength) {
    if ([TXAdapter shareAdapter].defaultType == tCurrentType())
        return defaultLength;
    
    BOOL isPhoneX = NO;
    if (@available(iOS 11.0, *)) {
        isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;
    }
    
    NSString *deviceType = [UIDevice currentDevice].model;
    if ([deviceType isEqualToString:@"iPad"]) {
        
        CGFloat xx =  [UIScreen mainScreen].bounds.size.height / tScreenHeight() * defaultLength;
        NSLog(@"%f",xx);
        return xx;
    }
    
    if ([UIScreen mainScreen].bounds.size.width == SCREEN_WIDTH_iPhoneX_XS_11Pro_12mini_13mini) {
            return defaultLength;
    }
    
    if (isPhoneX) {
        return [UIScreen mainScreen].bounds.size.width/tScreenWidth() * defaultLength;
    }else
    {
        if ([UIScreen mainScreen].bounds.size.width<=SCREEN_WIDTH_iPhone6_6S_7_8_SE_SE2_SE3 || [UIScreen mainScreen].bounds.size.width<=SCREEN_HEIGHT_iPhone6Plus_6SPlus_7Plus_8Plus) {
            return defaultLength;
        }
    }
    
//    414-360 = 54  多出了54个像素点
//    414/360 = 1.15倍 这是他的倍率
    
    //1.15*104 = 119.6
    //而在最低的图片标准中宽度要126才行
    
    //如果
    //54+119.6 = 173.6
    //
    
//    [TXAdapter shareAdapter].defaultScreenWidth
    return [UIScreen mainScreen].bounds.size.width/tScreenWidth() * defaultLength;
}


//专门需要适配iphone6以下的机型
static inline CGFloat zsHeight(CGFloat defaultHeight){
   
    if ([TXAdapter shareAdapter].defaultType == tCurrentType())
        return defaultHeight;
    return [UIScreen mainScreen].bounds.size.height/tScreenHeight() * defaultHeight;
}

static inline CGFloat zsWidth(CGFloat defaultWidht){
    if ([TXAdapter shareAdapter].defaultType == tCurrentType())
        return defaultWidht;
      
//    [TXAdapter shareAdapter].defaultScreenWidth
    return [UIScreen mainScreen].bounds.size.width/tScreenWidth() * defaultWidht;
}
